package com.ds.springboot.service;

import com.ds.springboot.mapper.DeptMapper;
import com.ds.springboot.pojo.Department;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: springboot-1-cache
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-05-20
 **/
@CacheConfig(cacheNames = "dept")
@Service
public class DeptServiceImpl implements DeptService{
    @Resource
    DeptMapper deptMapper;

    @Override
    @Cacheable(unless = "#result==null")
    public Department getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    @CachePut(key = "#department.id")
    public Department update(Department department) {
        int update = deptMapper.update(department);
        return department;
    }

    @Override
    public Department add(Department department) {
        int add = deptMapper.add(department);
        return department;
    }

    @Override
    @CacheEvict
    public int del(Integer id) {
        return deptMapper.del(id);
    }
}
